package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysDevicesUse;
import com.gec.model.vo.SysDevicesUseQueryVo;
import com.gec.system.service.SysDevicesUseService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 器材使用记录表 前端控制器
 * </p>
 *
 * @author lxj
 * @since 2024-03-10
 */
@Api(tags = "器材使用控制器")
@RestController
@RequestMapping("/admin/system/sysDevicesUse")
public class SysDevicesUseController {

    @Autowired
    private SysDevicesUseService sysDevicesUseService;


    @ApiOperation("获取全部")
    @GetMapping("/findAll")
    public Result findAll(){
        List<SysDevicesUse> list = this.sysDevicesUseService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据id去移除")
    // 测试删除
    @DeleteMapping("/removeDevicesUse/{id}")
    public Result removeDevicesUse(@PathVariable Long id)
    {
        boolean b = this.sysDevicesUseService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }

    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findDevicesUseByPageQuery(@PathVariable Long page,
                                         @PathVariable Long limit,
                                         SysDevicesUseQueryVo sysDevicesUseQueryVo)
    {
        //1.创建分页对象
        IPage<SysDevicesUse> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.sysDevicesUseService.selectPage(p1,sysDevicesUseQueryVo);
        //3.返回
        return Result.ok(p1);
    }
    // 添加
    @PostMapping("/addDevicesUse")
    public Result addDevicesUse(@RequestBody SysDevicesUse sysDevicesUse)
    {
        boolean res = this.sysDevicesUseService.save(sysDevicesUse);
        if (res)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 修改
    //1.根据id
    @GetMapping("/findDevicesUseById/{id}")
    public Result findDevicesUseById(@PathVariable Long id)
    {
        SysDevicesUse sysDevicesUse = this.sysDevicesUseService.getById(id);
        return Result.ok(sysDevicesUse);
    }

    // 实现修改
    @PostMapping("/updateDevicesUse")
    public Result updateDevicesUse(@RequestBody SysDevicesUse sysDevicesUse)
    {
        boolean b = this.sysDevicesUseService.updateById(sysDevicesUse);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @DeleteMapping("/removeDevicesUseByIds")
    public Result removeDevicesUseByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.sysDevicesUseService.removeByIds(ids);

        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}
